#include "Base.h"
#include "RenderManager.h"
	


CRenderManager::~CRenderManager()
{
}


void CRenderManager::Init()
{
	
}


void	EnableOpenGL			(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
	PIXELFORMATDESCRIPTOR pfd;

    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC(hwnd);

    /* set the pixel format for the DC */
    ZeroMemory(&pfd, sizeof(pfd));

    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;

    iFormat = ChoosePixelFormat(*hDC, &pfd);

    SetPixelFormat(*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}
	

void	DisableOpenGL			(HWND _hwnd, HDC _hDC, HGLRC _hRC)
{
	wglMakeCurrent(NULL, NULL);
    wglDeleteContext(_hRC);
    ReleaseDC(_hwnd, _hDC);
}



void CRenderManager::RenderSphere(float radio,const unsigned int vstacks,const unsigned int hstacks)
{
	float v_delta;
    float h_delta;
    float v_angle;
    float next_v_angle;


    v_delta=fPI/(float)vstacks;
    h_delta=(2*fPI)/(float)hstacks;



    for(unsigned int i=0;i<vstacks;i++)
    {
        v_angle=(fPI/2)-(i*v_delta);
        next_v_angle=(fPI/2)-((i+1)*v_delta);

        glBegin(GL_QUAD_STRIP);
        for(unsigned int j=0;j<=hstacks;j++)
        {
            glNormal3f(cos(v_angle)*cos(j*h_delta) , sin(v_angle) , cos(v_angle)*sin(j*h_delta));
            glVertex3f(cos(v_angle)*radio*cos(j*h_delta) , sin(v_angle)*radio , cos(v_angle)*radio*sin(j*h_delta));
            glNormal3f(cos(next_v_angle)*cos(j*h_delta) , sin(next_v_angle) , cos(next_v_angle)*sin(j*h_delta));
            glVertex3f(cos(next_v_angle)*radio*cos(j*h_delta) , sin(next_v_angle)*radio , cos(next_v_angle)*radio*sin(j*h_delta));
        }
        glEnd();
    }
}